import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class Game extends JPanel {

	/**
	 * @param args
	 */
	public static ChessMen[] whiteTeam = new ChessMen[16];
	public static ChessMen[] blackTeam = new ChessMen[16];

	public static boolean[][] checkBoard = new boolean[8][8];
	public static ChessMen[][] boardMen = new ChessMen[8][8];

	public static ChessMen selectedMen;
	public static int targetX;
	public static int targetY;

	public static Game t;

	Board b = new Board();

	public static void main(String[] args) throws InterruptedException {

		for (int i = 1; i < 9; i++) {
			whiteTeam[i - 1] = new ChessMen("pawn", Team.WHITE, i, 2);
			boardMen[i - 1][1] = whiteTeam[i - 1];

		}

		whiteTeam[8] = new ChessMen("rook", Team.WHITE, 1, 1);
		boardMen[0][0] = whiteTeam[8];

		whiteTeam[9] = new ChessMen("rook", Team.WHITE, 8, 1);
		boardMen[7][0] = whiteTeam[9];

		whiteTeam[10] = new ChessMen("knight", Team.WHITE, 2, 1);
		boardMen[1][0] = whiteTeam[10];
		whiteTeam[11] = new ChessMen("knight", Team.WHITE, 7, 1);
		boardMen[6][0] = whiteTeam[11];

		whiteTeam[12] = new ChessMen("bishop", Team.WHITE, 3, 1);
		boardMen[2][0] = whiteTeam[12];

		whiteTeam[13] = new ChessMen("bishop", Team.WHITE, 6, 1);
		boardMen[5][0] = whiteTeam[13];

		whiteTeam[14] = new ChessMen("king", Team.WHITE, 5, 1);
		boardMen[4][0] = whiteTeam[14];

		whiteTeam[15] = new ChessMen("queen", Team.WHITE, 4, 1);
		boardMen[3][0] = whiteTeam[15];

		for (int i = 1; i < 9; i++) {
			blackTeam[i - 1] = new ChessMen("pawn", Team.BLACK, i, 7);
			boardMen[i - 1][6] = blackTeam[i - 1];
		}

		blackTeam[8] = new ChessMen("rook", Team.BLACK, 1, 8);
		boardMen[0][7] = blackTeam[8];

		blackTeam[9] = new ChessMen("rook", Team.BLACK, 8, 8);
		boardMen[7][7] = blackTeam[9];

		blackTeam[10] = new ChessMen("knight", Team.BLACK, 2, 8);
		boardMen[1][7] = blackTeam[10];

		blackTeam[11] = new ChessMen("knight", Team.BLACK, 7, 8);
		boardMen[6][7] = blackTeam[11];

		blackTeam[12] = new ChessMen("bishop", Team.BLACK, 3, 8);
		boardMen[2][7] = blackTeam[12];

		blackTeam[13] = new ChessMen("bishop", Team.BLACK, 6, 8);
		boardMen[5][7] = blackTeam[13];

		blackTeam[14] = new ChessMen("king", Team.BLACK, 5, 8);
		boardMen[4][7] = blackTeam[14];

		blackTeam[15] = new ChessMen("queen", Team.BLACK, 4, 8);
		boardMen[3][7] = blackTeam[15];

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 2; j++) {
				checkBoard[i][j] = true;
			}
		}

		for (int i = 0; i < 8; i++) {
			for (int j = 6; j < 8; j++) {
				checkBoard[i][j] = true;
			}
		}

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				// System.out.print(checkBoard[j][i]);
				System.out.print(boardMen[j][i]);
			}
			System.out.println("");
		}

		// TODO Auto-generated method stub
		Handler handler = new Handler();
		JFrame f = new JFrame("Chess");
		f.setResizable(false);

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		t = new Game();
		t.addMouseListener(handler);
		f.add(t);
		f.setSize(700, 700);
		f.setVisible(true);

	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		this.setBackground(Color.WHITE);
		g.setColor(Color.BLUE);
		b.draw(g);

		for (int i = 0; i < 16; i++) {
			whiteTeam[i].draw(g);
			blackTeam[i].draw(g);
		}

		if (selectedMen != null) {
			g.setColor(Color.BLACK);

		}

	}

	private static class Handler implements MouseListener {
		public void mouseClicked(MouseEvent event) {
			if (selectedMen != null) {

				targetX = (event.getX() - 40) / 75 + 1;
				targetY = (event.getY() - 40) / 75 + 1;
				if (boardMen[targetX - 1][targetY - 1] == null) {
					boardMen[selectedMen.x - 1][selectedMen.y - 1] = null;
					selectedMen.x = targetX;
					selectedMen.y = targetY;
					boardMen[selectedMen.x - 1][selectedMen.y - 1] = selectedMen;
				}

				selectedMen = null;
				t.repaint();

			} else {
				int x = (event.getX() - 40) / 75;
				int y = (event.getY() - 40) / 75;
				System.out.println(x + " " + y);
				selectedMen = boardMen[x][y];

				if (selectedMen != null) {
					selectedMen.selected = true;

				}

				System.out.println(selectedMen);
			}

		}

		@Override
		public void mouseEntered(MouseEvent event) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent event) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent event) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}
	}

}
